package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Override
    public PageResponse<NursingPlanVo> findNursingPlanPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> list = nursingPlanMapper.findNursingPlanPage(name, status);
        list.getResult().forEach(nursingPlanVo ->
            nursingPlanVo.setCount(nursingLevelMapper.findNursingLevelByPlanId(nursingPlanVo.getId()))
        );
        return PageResponse.of(list,NursingPlanVo.class);
    }

    @Override
    @Transactional
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.addNursingPlan(nursingPlan);
        NursingPlan newNursingPlan = nursingPlanMapper.getNursingPlanByProjectName(nursingPlan.getPlanName());
        nursingPlan.getProjectPlans().forEach(item->{
            item.setPlanId(newNursingPlan.getId());
            nursingProjectPlanMapper.addNursingProjectPlan(item);
        });
    }

    @Override
    public NursingPlanVo findNursingPlanById(Long id) {
        NursingPlanVo nursingPlan = nursingPlanMapper.findNursingPlanById(id);
        nursingPlan.setProjectPlans(nursingProjectPlanMapper.findNursingProjectPlanByPlanId(id));
        return nursingPlan;
    }

    @Override
    @Transactional
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlanDto.getId());
        nursingPlan.getProjectPlans().forEach(item->{
            item.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.addNursingProjectPlan(item);
        });
    }

    @Override
    @Transactional
    public void deleteNursingPlan(Long id) {
        nursingPlanMapper.deleteNursingPlan(id);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(id);
    }

    @Override
    public void updateNursingPlanByStatus(Long id, Integer status) {
        nursingPlanMapper.updateNursingPlanByStatus(id,status);
    }

    @Override
    public List<NursingPlanVo> findAllNursingPlan() {
        return nursingPlanMapper.findAllNursingPlan();
    }
}
